using System.Collections.Generic;
using Game.Const;
using Game.Core;
using Game.Main.Model;
using Game.Manager;
using UnityEngine;

namespace Game.Component.Operation
{
	public class Game1Component : OperationComponent
	{
		private Transform btnBack;

		private List<Transform> efcs = new List<Transform>();

		private void Awake()
		{
			btnBack = base.transform.Find("BtnBack");
			int childCount = base.transform.childCount;
			List<Transform> list = new List<Transform>();
			for (int i = 0; i < childCount; i++)
			{
				if (base.transform.GetChild(i).name.StartsWith("Box"))
				{
					list.Add(base.transform.GetChild(i));
				}
			}
			Transform transform = base.transform.Find("GermDemo");
			childCount = Random.Range(4, 6);
			for (int j = 0; j < childCount; j++)
			{
				Transform transform2 = Object.Instantiate(transform.gameObject).transform;
				Transform transform3 = list[Random.Range(0, list.Count)];
				list.Remove(transform3);
				transform2.SetParent(transform3);
				transform2.localPosition = new Vector3(Random.Range(-0.4f, 0.4f), 0.6f, 0f);
				GermObject germObject = transform2.gameObject.AddComponent<GermObject>();
				germObject.clr = this;
				germObject.box = transform3;
				transform2.gameObject.SetActive(true);
			}
		}

		public override void TouchDown(TouchEventData touchEventData)
		{
			if (displayer == null)
			{
				return;
			}
			base.TouchDown(touchEventData);
			RaycastHit2D raycastHit2D = Physics2D.Raycast(touchEventData.curPos, Vector2.zero, 15f, 1);
			if (!(raycastHit2D.collider != null))
			{
				return;
			}
			if (raycastHit2D.collider.transform == btnBack)
			{
				Singleton<AudioManager>.Instance.PlaySound("sound_btn_1");
				displayer.ChangeType(OperationDisplayerItem.Type.Main);
				return;
			}
			GermObject component = raycastHit2D.collider.transform.GetComponent<GermObject>();
			if (component != null)
			{
				component.clr = this;
				component.PlayDestoryAnim();
				Singleton<AudioManager>.Instance.PlaySound("sound_btn_1");
			}
		}

		public void PlayCompleteEfc(Vector3 pos)
		{
			Singleton<AudioManager>.Instance.PlaySound("sound_blink");
			Transform transform = Singleton<EffectManager>.Instance.ShowEffect(PathConst.EFFECT + "efc_blink_1", pos + new Vector3(0f, 0f, -1E-05f), 3f);
			transform.SetParent(base.transform);
			efcs.Add(transform);
		}

		private void OnDestroy()
		{
			for (int i = 0; i < efcs.Count; i++)
			{
				if (efcs[i] != null)
				{
					Singleton<GamePoolManager>.Instance.Despawn(efcs[i]);
				}
			}
			efcs.Clear();
		}
	}
}
